<template>
	<view class="page">
		<cu-custom  bgColor="bg-white" :isBack="true" :izSharePage='izSharePage'><block slot="content">活动详情</block></cu-custom>
		<view class="flex-1" v-if="activity">
			<!-- <view class="image bg-img" style="height:82vh;width: 100%; background:url(https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg);"></view> -->
			<scroll-view scroll-y="true" style="height:82vh;">
				<view class="description">
					<view class="content">
						<view class="grace-rich-text">
						    <rich-text type="text" :nodes="intro"></rich-text>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="padding bg-white flex-1" style="position: fixed; bottom:0; width: 100%;">
				<view class="flex justify-between align-center">
					<view>
						<view class="text-333 text-df text-fw600">{{activity.active_title}}</view>
						<view class="flex align-end" style="margin-top: 10rpx;">
							<text class="text-theme text-df margin-right text-fw600" v-if="activity.active_price">￥{{activity.active_price | formatPrice}}/张</text>
							<text class="text-666 text-sm" style="margin-right:10rpx;padding-bottom: 2rpx;" v-if="activity.limit_buy_num>0">限购{{activity.limit_buy_num}}张/人</text>
							<text class="text-666 text-sm" style="padding-bottom: 2rpx;">剩余{{activity.enable_quantity}}张 </text>
						</view>
					</view>
					<view class="flex" @tap="buy">
						<text class="buy-btn text-df" :class="activity.operation=='NO'?'bg-theme':'bg-999'">{{activity.operation_text}}</text>
					</view>
				</view>
			</view>
			<view class="shareIcon bg-white" @tap="handleShare(activity.share_image,activity.share_url)">
				<view class='flex line-grey share-tag align-center'>
					<text class='cuIcon-share'></text>
					<text class="text-sm">分享</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as activity_API from '@/api/tour.js'
	import graceRichText from '@/common/richText.js'
	import {share,gup,filterUrlParam} from '@/utils/Foundation.js'
	import * as Main_API from '@/api/main/home.js'
	export default {
		data() {
			return {
				active_id:null,
				activity:{},
				intro:'',
				miniQrcode:null,
				izSharePage:false
			};
		},
		onLoad(e) {
			if (e.scene) {
				// console.log(e)
				let query = filterUrlParam(decodeURIComponent(e.scene))
				if(query.s){
					this.getShopInfo(query.s)
				}
				if(query.a){
					this.active_id = query.a
				}
				if(query.r){
					uni.setStorageSync('referrer',{'phone':query.r})
				}
			} 
			if(e.active_id){
				this.active_id = e.active_id
				console.log(this.active_id) 
			}
			if (e.q) {
				this.izSharePage = true
			    let queryAll = decodeURIComponent(e.q);
				console.log(filterUrlParam(decodeURIComponent(e.q)))
				let r_phone = gup('r_phone', queryAll)
			    let shop_id = gup('shop_id', queryAll)
			    this.active_id = gup('active_id', queryAll)
				if(r_phone) uni.setStorageSync('referrer',{'phone':r_phone})
				if(shop_id) this.getShopInfo(shop_id)
			} 
		},
		onShow() {
			this.getactivityDetail()
		},
		onBackPress() {
			this.izSharePage = false
		},
		methods:{
			onShareAppMessage() {
				let title = this.activity.active_title
				let page_path = 'pages/activity/tour/zone'
				let mobile = uni.getStorageSync('userInfo').mobile
				let user = uni.getStorageSync('userInfo').nickname
				let shop_name = uni.getStorageSync('shop').shop_name
				let shop_id = uni.getStorageSync('shop').shop_id
				let path = ''
				if(mobile && shop_id){
					path = page_path+'?active_id='+this.active_id+'&shop_id='+shop_id+'&sharePhone='+mobile
				}else if(shop_id){
					path =  page_path+'?active_id='+this.active_id+'&shop_id='+shop_id
				}
				console.log('12',path)
			    return {
					title: title,
					path: path
			    }
			},
			getShopInfo(shop_id){
				Main_API.getShopInfo(shop_id).then(async res=>{
					if(res.shop_id){
						this.shop_id = res.shop_id
						this.shop_name = res.shop_name
						await uni.setStorageSync('shop',res)
					}
				}).catch(()=>{
					uni.showLoading({
						title:'店铺已不存在，请换一个试试'
					})
				})
			},
			getactivityDetail(){
				activity_API.getTourDetail(this.active_id).then(res =>{
					this.activity = res
					this.intro = graceRichText.format(res.active_desc)
				})
			},
			buy(){
				if(this.activity.operation=='NO'){
					if(!uni.getStorageSync('token')){
						uni.navigateTo({url:'/pages/login/login'})
					}else{
						uni.navigateTo({
							url:'./orderConfirm?active_id='+this.active_id
						})
					}
				}
			},
			handleShare(shareImg,shareUrl){
				let shop_id = uni.getStorageSync('shop').shop_id
				activity_API.getQrcode(this.active_id,shop_id).then(res=>{
					this.miniQrcode = res
					uni.setStorage({
						key:'sharePoster',
						data:{
							share_image:shareImg,
							miniQrcode:this.miniQrcode || res
						}
					})
					uni.navigateTo({
						url:'../../commons/poster'
					})
				})
			}	
		}
	}
</script>

<style lang="scss" scoped>
.buy-btn{
	padding: 10rpx 20rpx 10rpx 28rpx;
	border-radius: 10rpx;
	overflow: hidden;
	font-size: 26rpx;
	letter-spacing:8rpx;
}
.shareIcon{
	position: fixed;
	bottom:200rpx; 
	right: 0;
	border-radius: 20rpx 0 0 20rpx;
	overflow: hidden;
	border: 1px solid #999;
	.share-tag{
		padding: 10rpx 20rpx;
	}
}
.description {
	margin-bottom: 10upx;
}
.grace-rich-text{
	padding:10upx 20upx;
	img{
		margin: 0;
	}
}
.bg-999{
	background: #E1E1E1;
	color:#FFF;
}
</style>
